package com.maoxianzhe.gonghui.common.HandlerInterceptor;

import com.maoxianzhe.gonghui.common.Exception.BusinessException;
import com.maoxianzhe.gonghui.common.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理、数据预处理等
 */
@ControllerAdvice
public class ExceptionInterceptor {

    private static final Logger LOG = LoggerFactory.getLogger(ExceptionInterceptor.class);

    /**
     * 所有异常统一处理
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        LOG.error("系统异常：", e);
        return Result.error(e.getMessage());
    }

    /**
     * 业务异常统一处理
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Result exceptionHandler(BusinessException e) {
        LOG.error("业务异常：", e);
        return Result.error(e.getMessage());
    }

//    /**
//     * 校验异常统一处理
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(value = Exception.class)
//    @ResponseBody
//    public Result exceptionHandler(Exception e) {
//        Result result = new Result();
//        LOG.error("系统异常：", e);
//        result.setCode(0);
//        result.setMsg("系统出现异常，请联系管理员");
//        return result;
//    }

}
